//
//  UseTimeDayChartView.swift
//  Easepods
//
//  Created by 李然 on 2024/6/15.
//

import UIKit
import EarbudsDatabase

class UseTimeDayChartView: View {
    
    public var data: [UsetimeDetailModel] = [] {
        didSet {
            setNeedsDisplay()
        }
    }
    
    /// x轴文字颜色
    public var xAxisLabelColor: UIColor = .lightGray
    
    /// x轴虚线颜色
    public var xAxisLineColor: UIColor = .lightGray
    
    public var labelAttr: [NSAttributedString.Key: Any] {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 6
        return [.foregroundColor: xAxisLabelColor,
                .font: UIFont.systemFont(ofSize: 10),
                .paragraphStyle: paragraphStyle]
    }
    
    open override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = .clear
    }
    
    open override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        drawXAxisValuesAndFullLines(rect)
        
        drawSegmentLine(rect)
    }
    
    open func drawXAxisValuesAndFullLines(_ rect: CGRect) {
        
        guard let context = UIGraphicsGetCurrentContext() else { return }

        context.saveGState()
        defer { context.restoreGState() }
        
        let xValues = ["0", "6", "12", "18", "24"]
        
        var xStart: CGFloat = 20
        let xMargin = (rect.width - 40) / 4
        
        for xValue in xValues {
            let xValueHeight = xValue.size(withAttributes: labelAttr).height
            context.drawText(xValue, at: CGPoint(x: xStart, y: rect.height - xValueHeight), align: .center, attributes: labelAttr)
            
            context.setStrokeColor(xAxisLineColor.cgColor)
            let dottieLinePath = UIBezierPath()
            dottieLinePath.lineWidth = 0.5
            dottieLinePath.setLineDash([1.0, 1.0], count: 1, phase: 0)
            dottieLinePath.move(to: CGPoint(x: xStart, y: rect.minY))
            dottieLinePath.addLine(to: CGPoint(x: xStart, y: rect.height - xValueHeight - 3))
            dottieLinePath.stroke()
            
            xStart += xMargin
        }
    }
    
    open func drawSegmentLine(_ rect: CGRect) {
        
        guard let context = UIGraphicsGetCurrentContext() else { return }

        context.saveGState()
        defer { context.restoreGState() }
        
        for time in self.data {
            context.setFillColor(time.useType.lineColor?.cgColor ?? UIColor.clear.cgColor)
            
            let startX = (CGFloat(time.startTimeMin) / 60 / 24) * (rect.width - 40) + 20
            let width =  (CGFloat(time.useTimeMin) / 60 / 24) * (rect.width - 40)
            
            let rect = CGRect(x: startX, y: rect.maxY / 2 - 3, width: width, height: 6)
            let path = UIBezierPath(roundedRect: rect, cornerRadius: 0)
            path.fill()
        }
    }
}

extension UsetimeUseType {
    var lineColor: UIColor? {
        switch self {
        case .audio: return R.color.use_time_3_media()
        case .call: return R.color.use_time_2_phone()
        case .standby: return R.color.use_time_4_unused()
        case .shutdown: return .clear
        }
    }
}
 
